package tech.waterism.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Jackson字符串转换的格式化将
 * 将日期格式为"yyyy-MM-dd HH"的字符转换为Jackson可以识别的格式
 * 同时,将汉字的字符串进行解码
 *
 * @author Administrator
 */
public class JacksonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private static SimpleDateFormat jacksonSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    public static String writeValueAsString(Object arg0) {
        String fmtJson = "";
        //时间正则匹配, 格式为"yyyy-MM-dd HH"
        Pattern pDate = Pattern.compile("(\\d{1,4}[-|\\/]\\d{1,2}[-|\\/]\\d{1,2} \\d{1,2})", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
//		//汉字正则匹配
//		Pattern pChinese = Pattern.compile("[\\u4e00-\\u9fa5]+", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
//		//正则匹配, 取出所有在Name":"与",之间的字符串, 忽略name的大小写
//		Pattern pString = Pattern.compile("(?i)Name\":\"(.*?)\"|(?i)gateNote\":\"(.*?)\"", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
        try {
            String objJson = objectMapper.writeValueAsString(arg0);

            //将日期格式为"yyyy-MM-dd HH"的字符转换为Jackson可以识别的格式
            Matcher matcherDate = pDate.matcher(objJson);
            while (matcherDate.find()) {
//	        	System.out.println(matcherDate.group());
                for (int i = 1; i <= matcherDate.groupCount(); i++) {
                    String temp = matcherDate.group(i);
                    Date dtTime = sdf.parse(temp);
                    String fmtTemp = jacksonSdf.format(dtTime);
//	                System.out.println(fmtTemp+"----");
                    //针对加号, 格式化, 解决加号变空格的情况
                    fmtTemp = fmtTemp.replaceAll("\\+", "%2B");
//	                System.out.println(fmtTemp+"____________");
                    objJson = objJson.replaceAll(temp, fmtTemp);

                }
            }
//	        Matcher matcherString = pString.matcher(objJson);
//			while (matcherString.find()) {
//				for (int i = 1; i <= matcherString.groupCount(); i++) {
//					String str = matcherString.group(i);
//					if (str != null) {
//						boolean isChinese = pChinese.matcher(str).matches();
//						// 若不是汉字,则需要解码
//						if (!isChinese) {
//							String strDecode = URLDecoder.decode(str, "UTF-8");
//							objJson = objJson.replaceAll(str, strDecode);
//						}
//					}
//					
//				}
//			}    
//	        System.out.println(objJson+"**********");
            fmtJson = URLDecoder.decode(objJson, "UTF-8");
//	        System.out.println(fmtJson+"**********");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fmtJson;
    }

    public static <T> T convert(Object object, Class<T> valueType) {
        try {
            String jsonStr = JacksonUtil.writeValueAsString(object);
            return objectMapper.readValue(jsonStr, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println("1111111111111111111111111111111111111111111");
        return null;
    }

}
